package com.yuanxiao;

import com.yuanxiao.server.RpcNetty;
import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ServerBootstrap {
    public static void main(String[] args) throws InterruptedException {
        SpringApplication.run(ServerBootstrap.class,args);
        //注册到zk
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
        CuratorFramework client1 = CuratorFrameworkFactory.builder()
                .connectString("192.168.0.105:2181") //server地址
                .sessionTimeoutMs(5000) // 会话超时时间
                .connectionTimeoutMs(8000) // 连接超时时间
                .retryPolicy(retryPolicy) // 重试策略
                .namespace("base") // ᇿ⽴命名空间/base
                .build(); //
        client1.start();
        System.out.println("Zookeeper session established. ");
        try {
            String path ="/zkServer/z1";
            client1.create().creatingParentContainersIfNeeded().withMode(CreateMode.EPHEMERAL_SEQUENTIAL).forPath(path,"127.0.0.1:8990".getBytes());
            System.out.println("success create znode"+path);
        } catch (Exception e) {
            e.printStackTrace();
        }

        RpcNetty.startServer("127.0.0.1",8990);
    }
}
